package com.wyzt.user.service;

import com.wyzt.common.enums.ExceptionEnum;
import com.wyzt.common.exception.WyztException;
import com.wyzt.common.utils.NumberUtils;
import com.wyzt.user.mapper.UserMapper;
import com.wyzt.user.pojo.User;
import com.wyzt.user.utils.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "user:verify:phone:";

    public Boolean checkData(String data, Integer type) {
        User user = new User();
        //判断数据类型
        switch (type) {
            case 1: //验证用户名
                user.setUsername(data);
                break;
            case 2: //验证手机号
                user.setPhone(data);
                break;
            default:
                throw new WyztException(ExceptionEnum.USER_DATA_TYPE_ERROR);
        }

        //唯一性校验
        return userMapper.selectCount(user) == 0;
    }

    /**
     * 发送短信
     *
     * @param phone
     */
    public void sendCode(String phone) {
        String key = KEY_PREFIX + phone;
        //生成验证码
        String code = NumberUtils.generateCode(6);
        Map<String, String> msg = new HashMap<>();
        msg.put("phone", phone);
        msg.put("code", code);
        System.out.println("code" + code);
        //发送短信验证码
        amqpTemplate.convertAndSend("wyzt.sms.exchange", "sms.verify.code", msg);
        //保存验证码
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);

    }

    /**
     * 注册
     *
     * @param user
     * @param code
     */
    public void register(User user, String code) {
        //从redis取出验证码
        String cacheCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        //校验验证码
        if (!StringUtils.equals(code, cacheCode)) {
            throw new WyztException(ExceptionEnum.INVALID_VERIFY_CODE);
        }
        //生成盐
        String salt = Md5Utils.generate();
        //对密码进行加密
        String md5Password = Md5Utils.encryptPassword(user.getPassword(), salt);
        user.setSalt(salt);
        user.setPassword(md5Password);
        user.setCreated(new Date());
        //写入数据库
        userMapper.insert(user);
    }

    public User queryUserByUsernameAndPassword(String username, String password) {
        User user = new User();
        user.setUsername(username);
        User result = userMapper.selectOne(user);
        //校验
        if (result == null) {
            throw new WyztException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        //校验密码
        if (StringUtils.equals
                (result.getPassword(), Md5Utils.encryptPassword(password, result.getSalt()))) {
            throw new WyztException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        //返回结果
        return result;
    }
}
